﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BL;
using BO;

namespace AnimalFarm
{
    public partial class AddAuctionMarket : Form
    {
        B_AuctionMarket blObject = null;
        DataTable dTable = null;
        public AddAuctionMarket()
        {
            InitializeComponent();
            rdpDate.Value = DateTime.Now;
            txtAuctionCode.Text = GetRandomCode();
            blObject = B_AuctionMarket.CreateInstance();
            LoadAllAuctionMarket();
        }

        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        private string RandomString(int size, bool lowerCase)
        {
            StringBuilder strBuilder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                strBuilder.Append(ch);
            }
            if (lowerCase)
                return strBuilder.ToString().ToLower();
            return strBuilder.ToString();
        }

        //This code generates a code of length 10 with first 4 letters lowercase,
        //next 4 letters numbers, and last 2 letters as uppercase.
        public string GetRandomCode()
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append(RandomString(4, true));
            strBuilder.Append(RandomNumber(1000, 9999));
            strBuilder.Append(RandomString(2, false));
            return strBuilder.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string auctionCode = txtAuctionCode.Text;
            string auctionName = txtAuctionName.Text;
            DateTime auctionDate = rdpDate.Value;
            if (auctionName.Length > 0)
            {
                AuctionMarket_Info am = new AuctionMarket_Info();

                am.AuctionCode = auctionCode;
                am.AuctionMarketName = auctionName;
                am.AuctionMarketDate = auctionDate;                

                bool isEffected = blObject.CreateOneAuctionMarket(am);

                if (isEffected == true)
                {
                    lblInfo.Text = "Create a new auction market successfully!";
                    txtAuctionCode.Text = GetRandomCode();
                    txtAuctionName.Text = "";
                    rdpDate.Value = DateTime.Now;
                    LoadAllAuctionMarket();
                }
                else
                {
                    lblInfo.Text = "Cannot create a new auction market!";
                }
            }
            else
            {
                MessageBox.Show("Auction market name must not be empty", "Missing data", MessageBoxButtons.OK);
                txtAuctionName.Focus();
            }
        }


        private void LoadAllAuctionMarket()
        {
            dTable = blObject.GetAllAuctionMarket();
            rgvOwnershipHistory.DataSource = dTable;            
        }

        private void AddAuctionMarket_Load(object sender, EventArgs e)
        {

        }

        private void btnAddOwnershipHistory_Click(object sender, EventArgs e)
        {
            int index = rgvOwnershipHistory.CurrentRow.Index;
            if (index >= 0)
            {
                //MessageBox.Show(index.ToString());
                if (dTable != null)
                {
                    DataRow row = dTable.Rows[index];
                    MessageBox.Show(row["AuctionMarketID"].ToString());
                }
            }
        }
    }
}
